/* SPDX-License-Identifier: GPL-2.0+
 *
 * (C) Copyright 2020
 * Anton Puiu, anton.puiu@email.com
 */

#ifndef SO_FUNCTIONS_H
#define SO_FUNCTIONS_H

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>

#include <stdlib.h>
#include <stdio.h>

#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#ifdef DEBUG
#include <stdio.h>
#endif

#include "so_stdio.h"
#include "../types/last_op.h"

#include "../mode/mode.h"

#define PERMISSIONS 0644
#define BUF_SIZE 4096

#define ACCESS_DENIED NULL
#define ACCESS_GRANTED 1

static inline void *so_free(void *addr, ...)
{
	va_list list;
	void *p;

	va_start(list, addr);

	p = addr;

	while (p != NULL) {
		free(p);
		p = va_arg(list, void *);
	}

	va_end(list);

	return NULL;
}

static inline SO_FILE *so_access(SO_FILE *file, const char *pathname, int mode)
{
	if (access(pathname, mode) == -1) {
		#ifdef DEBUG2
		if (mode == F_OK)
			printf(FILE_NOT_FOUNT_ERROR);
		else if (mode == R_OK)
			printf(FILE_CANNOT_READ_ERROR);
		else if (mode == W_OK)
			printf(FILE_CANNOT_WRITE_ERROR);
		#endif

		return NULL;
	}

	return file;
}

#endif
